/*--------------------------------------------------------------------------------------*\
**
** blacksmith_window.cpp
**
** Heroes IV
** Copyright 2000, The 3DO Company
**
\*--------------------------------------------------------------------------------------*/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "blacksmith_window.h"

#include "abbreviate_number.h"
#include "adaptor_handler.h"
#include "adventure_map.h"
#include "army.h"
#include "army_dialog.h"
#include "artifact_keyword.h"
#include "artifact_type.h"
#include "artifact_properties.h"
#include "bitmap_group_cache.h"
#include "bitmap_group_ptr.h"
#include "bitmap_group_window.h"
#include "blended_bitmap_window.h"
#include "bound_handler.h"
#include "building_traits.h"
#include "button.h"
#include "button_cache.h"
#include "creature_array_window.h"
#include "creature_detail_display.h"
#include "creature_traits.h"
#include "creature_type.h"
#include "debug_message.h"
#include "default_artifact_properties.h"
#include "default_artifacts.h"
#include "external_string.h"
#include "foreign_creature_array_window_controls.h"
#include "format_string.h"
#include "game_replace_keywords.h"
#include "hero.h"
#include "interface_help.h"
#include "mouse_event.h"
#include "platform.h"
#include "player.h"
#include "standard_fonts.h"
#include "string_insensitive_compare.h"
#include "text_window.h"
#include "toggle_button.h"
#include "town.h"
#include "town_properties.h"

extern	t_external_string const k_hotkey_buy( "buy.hot_keys" );
extern  t_button_cache			g_ok_button;
extern  t_button_cache const    k_buy_button;

t_button_cache k_l_arrow( "dialog.blacksmith.l_arrow" );
t_button_cache k_r_arrow( "dialog.blacksmith.r_arrow" );
t_bitmap_group_cache k_center_box( "dialog.blacksmith.center_box" );

t_bitmap_group_cache     k_blacksmith_background_bitmaps ("dialog.blacksmith.layout");
t_bitmap_group_cache     k_blacksmith_picture_bitmaps[] =
{
	"dialog.blacksmith.life",
	"dialog.blacksmith.order",
	"dialog.blacksmith.death",
	"dialog.blacksmith.chaos",
	"dialog.blacksmith.nature",
	"dialog.blacksmith.might",
	"dialog.blacksmith.generic",
};

namespace { // unnamed namespace
									  
}; // end unnamed namespace

/*
** Blacksmith item/potion tables.
*/

// Paladins - Good/life
static t_artifact_type table_order_paladins_potions [] =
{
    k_artifact_potion_of_restoration,
    k_artifact_holy_water,
    k_artifact_potion_of_healing,
    k_artifact_potion_of_mirth,
    k_artifact_potion_of_immortality,
    k_artifact_none
};

static t_artifact_type table_order_paladins_items [] =
{
    k_artifact_plate_mail, // plate armor
    k_artifact_longsword,
    k_artifact_none
};

// Alchemist's shop - Order
static t_artifact_type table_alchemist_shop_potions[] =
{
    k_artifact_potion_of_restoration,
    k_artifact_potion_of_immortality,
    k_artifact_potion_of_cold,
    k_artifact_binding_liquid,
    k_artifact_potion_of_fire_resistance,
    k_artifact_none
};

static t_artifact_type table_alchemist_shop_items[] =
{
    k_artifact_chainmail,
    k_artifact_mages_staff,
    k_artifact_telescope,
    k_artifact_none
};

// Armory - Evil/death
static t_artifact_type table_armory_potions[] =
{
    k_artifact_potion_of_restoration,
	k_artifact_vial_of_poison,
    k_artifact_cloud_of_despair,
    k_artifact_vial_of_choking_gas,
	k_artifact_potion_of_immortality,
    k_artifact_none
};

static t_artifact_type table_armory_items[] =
{
    k_artifact_chainmail,
    k_artifact_longsword,
    k_artifact_shield,
    k_artifact_none
};
 
// Rogue/Warlock's Guild - Chaos
static t_artifact_type table_chaos_town_potions[] =
{
    k_artifact_potion_of_restoration,
    k_artifact_kreegan_fire,
    k_artifact_vial_of_acid,
    k_artifact_potion_of_immortality,
    k_artifact_potion_of_mana,
    k_artifact_none
};

static t_artifact_type table_chaos_town_items[] =
{
	k_artifact_crossbow,
	k_artifact_greatsword,
    k_artifact_none
};

// Ranger's Guild - Nature
static t_artifact_type table_ranger_guild_potions[] =
{
    k_artifact_potion_of_restoration,
    k_artifact_potion_of_immortality,
    k_artifact_potion_of_strength,
    k_artifact_potion_of_health,
    k_artifact_potion_of_mana,
    k_artifact_none
};

static t_artifact_type table_ranger_guild_items[] =
{
	k_artifact_leather_armor,
	k_artifact_longbow,
	k_artifact_shield,
    k_artifact_none
};

// Arsenal - Neutral/Might
static t_artifact_type table_arsenal_potions[] =
{
    k_artifact_potion_of_restoration,
    k_artifact_potion_of_resistance,
    k_artifact_potion_of_endurance,
    k_artifact_potion_of_quickness,
    k_artifact_potion_of_immortality,
    k_artifact_none
};

static t_artifact_type table_arsenal_items[] =
{
	k_artifact_axe,
	k_artifact_greatsword,
	k_artifact_leather_armor,
	k_artifact_shield,
    k_artifact_none
};

struct t_blacksmith_table
{
    t_artifact_type     *potions_table_ptr;
    t_artifact_type     *items_table_ptr;
};

/*
** Blacksmith items table pointers.
*/
t_blacksmith_table g_blacksmith_table [] =
{
    { table_order_paladins_potions, table_order_paladins_items },
    { table_alchemist_shop_potions, table_alchemist_shop_items },
    { table_armory_potions, table_armory_items },
    { table_chaos_town_potions, table_chaos_town_items },
    { table_ranger_guild_potions, table_ranger_guild_items },
    { table_arsenal_potions, table_arsenal_items },
};

/*
** Create new button from layer bitmaps.
*/
t_button* create_blacksmith_button( std::string const& name, t_bitmap_group_ptr bitmap_ptr, t_window *parent_window )
{
	// create formation buttons
	t_button*		      button;
	t_window*             window;
	t_bitmap_layer const* layer;
	t_screen_rect         rect;

	layer = bitmap_ptr->find( name + "_released" );
	rect = layer->get_rect();
	button = new t_button( rect.top_left(), parent_window );
	window = new t_bitmap_layer_window( layer, -rect.top_left(), button );
	button->set_released_image( window );
	layer = bitmap_ptr->find( name + "_pressed" );
	if (layer != 0 )
	{
	    window = new t_bitmap_layer_window( layer, -rect.top_left(), button );
    	button->set_pressed_image( window );
    }
    else
    {
	    layer = bitmap_ptr->find( name + "_depressed" );
    	if (layer != 0 )
    	{
    	    window = new t_bitmap_layer_window( layer, -rect.top_left(), button );
        	button->set_pressed_image( window );
        }
    }

   	layer = bitmap_ptr->find( name + "_disabled" );
	if (layer != 0 )
	{
		window = new t_bitmap_layer_window( layer, -rect.top_left(), button );
		button->set_disabled_image( window );
	}
	layer = bitmap_ptr->find( name + "_highlighted" );
	if (layer != 0 )
	{
		window = new t_bitmap_layer_window( layer, -rect.top_left(), button );
		button->set_highlighted_image( window );
	}
	button->update_size();

	return button;
}

/*---------------------------------------------------------------------------------------*\
**
** Blacksmith window constructor.  (in town)
**
\*---------------------------------------------------------------------------------------*/
t_blacksmith_window::t_blacksmith_window( t_town const* town, t_army const* adjacent_army, t_adventure_frame *frame,
										  t_window* parent )
                   : t_window( k_completely_transparent, parent )
{
	t_screen_rect rect;
    int i;

    /*
    ** Init values.
    */
	m_town = (t_town *)town;
	m_adj_army_ptr = (t_army* )adjacent_army;
    m_frame = frame;

    for (i=0; i<k_max_items; i++)
    {
        m_items[i].number_window_ptr = NULL;
        m_items[i].spot_taken = false;
    }
    for (i=0; i<k_max_potions; i++)
    {
        m_potions[i].number_window_ptr = NULL;
        m_potions[i].spot_taken = false;
    }
    
	m_player_ptr = &m_town->get_map()->get_player(); //m_adj_army_ptr->get_owner(); 
	m_player_funds = m_player_ptr->get_funds()[k_gold];
	m_current_cost = 0;
    
	/*
    ** Load bitmaps, set position.
    */
	t_screen_rect parent_rect;

	m_background_bitmaps_ptr = k_blacksmith_background_bitmaps.get();
	parent_rect = get_parent()->get_client_rect();
	rect = m_background_bitmaps_ptr->get_rect();
	rect += (parent_rect.size() - rect.size()) / 2;
	init( rect );

	/*
    ** Create background.
    */
	t_bitmap_layer const*   layer;
	t_window*               window;
	t_window*               background;
	t_window*               bg_picture_ptr;
	t_screen_point          origin(0,0);
	t_text_window*          text_window_ptr;

	layer = m_background_bitmaps_ptr->find( "top" );
	window = new t_bitmap_layer_window( layer, origin, this );
	layer = m_background_bitmaps_ptr->find( "left" );
	window = new t_bitmap_layer_window( layer, origin, this );
	layer = m_background_bitmaps_ptr->find( "bottom" );
	window = new t_bitmap_layer_window( layer, origin, this );
	layer = m_background_bitmaps_ptr->find( "right" );
	window = new t_bitmap_layer_window( layer, origin, this );

	layer = m_background_bitmaps_ptr->find( "background" );
	background = new t_bitmap_layer_window( layer, origin, this );
	origin = -layer->get_rect().top_left();

    if (!m_adj_army_ptr)
    {
        layer = m_background_bitmaps_ptr->find( "border" );
       	window = new t_bitmap_layer_window( layer, origin, background );
    }

    // Get bitmaps from correct town.
	m_picture_bitmaps_ptr = k_blacksmith_picture_bitmaps[town->get_type()].get();

    // Get background picture.
	layer = m_picture_bitmaps_ptr->find( "bg_picture" );
	rect = m_background_bitmaps_ptr->find( "picture" )->get_rect(); // + origin;
	bg_picture_ptr = new t_bitmap_layer_window( layer, rect.top_left(), this );

    // Show title.
	rect = m_background_bitmaps_ptr->find( "title_banner" )->get_rect(); // + origin;
	text_window_ptr= new t_text_window( get_font( rect.height()), rect, this, 
                    ::get_traits( m_town->get_type(), k_town_blacksmith ).name,
                    t_pixel_24(0,0,0) );
	text_window_ptr->set_center_horizontal();

    /*
    ** Show item icons and cost on background - don't do this if no heroes???
    */

    // Create item displays.
    t_artifact_type     *item_ptr = g_blacksmith_table[m_town->get_type()].items_table_ptr;
    int count = 0;

    while (*item_ptr != k_artifact_none)
    {
        create_item_display( *item_ptr, bg_picture_ptr, &m_items[count] );
        item_ptr++;
        count++;
    }
    
    // Create potion displays
    t_artifact_type     *potion_ptr = g_blacksmith_table[m_town->get_type()].potions_table_ptr;

    count = 0;
    while (*potion_ptr != k_artifact_none)
    {
        create_item_display( *potion_ptr, bg_picture_ptr, &m_potions[count] );
        potion_ptr++;
        count++;
    }

    // Show cost text.
	rect = m_background_bitmaps_ptr->find( "kingdom_gold_text" )->get_rect();
	//m_total_gold_text_ptr = new t_text_window( get_font( rect.height() ), rect, this,
	m_total_gold_text_ptr = new t_text_window( get_font( 16 ), rect, this,
                    abbreviate_number(m_player_funds, rect.width()), t_pixel_24(0,0,0) );
	m_total_gold_text_ptr->set_center_horizontal();
    
	rect = m_background_bitmaps_ptr->find( "total_spent_text" )->get_rect();
	m_cost_window_ptr = new t_text_window( get_font( 22 ), rect, this,
                    format_string ("%i", m_current_cost ), t_pixel_24(0,0,0) );
	m_cost_window_ptr->set_center_horizontal();
    
    /*
    ** Create buttons.
    */
	t_button            *button_ptr;
	t_screen_point      point;
	t_help_block const& shared_help_block = get_help_block( "shared" );

	rect = m_background_bitmaps_ptr->find( "close_button" )->get_rect();
	button_ptr = new t_button( g_ok_button.get(), rect.top_left(), this );
	button_ptr->set_click_handler( bound_handler( *this, &t_blacksmith_window::close_click ));
	set_help( button_ptr, shared_help_block, "ok" );

	point = m_background_bitmaps_ptr->find( "purchase_button_pressed" )->get_rect().top_left();
	m_buy_button_ptr = new t_button( k_buy_button.get(), point, this );
	m_buy_button_ptr->set_click_handler( bound_handler( *this, &t_blacksmith_window::buy_click ));
  m_buy_button_ptr->enable (false);
	set_help( m_buy_button_ptr, shared_help_block, "buy" );	

  // Show creatures/armies.
  create_creature_windows( );
}

/*
** Show item/potion up/down arrow, quantity, and cost.
*/
void t_blacksmith_window::create_item_display( t_artifact_type item_type, t_window* bg_window_ptr, t_blacksmith_item *item )
{
	t_screen_rect       rect;
    char                button_name[128];
    char                icon_name[128];
	t_screen_point      origin(0,0);
	t_help_block const& help = get_help_block( "blacksmith" );

    //t_blacksmith_item *item = new t_blacksmith_item;

    // Find position in bitmap for item - not button...icon?
 	rect = m_picture_bitmaps_ptr->find( get_keyword( item_type ) )->get_rect();

    /*
    ** Create button for artifact item.  Also increments quantity.
    */
    sprintf ( icon_name, "dialog.blacksmith.%s", get_keyword(item_type) );
	item->icon_button_ptr = new t_button( (t_button_cache (icon_name)).get(),
        rect.top_left(), bg_window_ptr );
	item->icon_button_ptr->enable (true); //false);
	item->icon_button_ptr->set_help_balloon_text( get_name( item_type ) );
	item->icon_button_ptr->set_right_click_text( ::get_help_text( item_type ) );
	item->icon_button_ptr->set_click_handler( add_2nd_argument( bound_handler( *this, &t_blacksmith_window::item_clicked_up ), item));
	item->icon_button_ptr->update_size();

    /*
    ** Add left button handler.
    ** Find location of arrow (subtract quantity).
    */
    sprintf ( button_name, "l_arrow_%s", get_keyword( item_type ));
 	rect = m_picture_bitmaps_ptr->find( button_name )->get_rect();
	item->down_button_ptr = new t_blacksmith_button( item->icon_button_ptr, k_l_arrow.get(), rect.top_left(), bg_window_ptr );
	item->down_button_ptr->set_click_handler( add_2nd_argument( bound_handler( *this, &t_blacksmith_window::item_clicked_down ), item));
	set_help( item->down_button_ptr, help, "item_down" );

    /*
    ** Add right button handler (increment quantity).
    */
    sprintf ( button_name, "r_arrow_%s", get_keyword( item_type ));
 	rect = m_picture_bitmaps_ptr->find( button_name )->get_rect();
	item->up_button_ptr = new t_blacksmith_button( item->icon_button_ptr, k_r_arrow.get(), rect.top_left(), bg_window_ptr );
	item->up_button_ptr->set_click_handler( add_2nd_argument( bound_handler( *this, &t_blacksmith_window::item_clicked_up ), item));
	set_help( item->up_button_ptr, help, "item_up" );
    
    item->item_type = item_type; 
    item->spot_taken = true;
    item->quantity = 0;

    /*
    ** Show quantity text.
    */
	t_bitmap_group_ptr	box_bitmaps_ptr = k_center_box.get();
	t_bitmap_layer *    layer;
	t_window *          window_ptr;
    
	layer = box_bitmaps_ptr->find( "box" );

    sprintf ( button_name, "number_%s", get_keyword( item_type ));
 	rect = m_picture_bitmaps_ptr->find( button_name )->get_rect();
    origin = rect.top_left();
	rect = box_bitmaps_ptr->find( "box" )->get_rect();
    rect += origin;
	window_ptr = new t_bitmap_layer_window( layer, rect.top_left(), bg_window_ptr );
	item->number_window_ptr = new t_text_window( get_font( ((rect.height()*2)/3) ), rect,
	  	                 bg_window_ptr, format_string ("%i", item->quantity ), t_pixel_24(0,0,0) );
	item->number_window_ptr->set_center_horizontal();
	item->number_window_ptr->set_center_vertical();

    /*
    ** Show cost text.
    */
    sprintf ( button_name, "text_%s", get_keyword( item_type ));
 	rect = m_picture_bitmaps_ptr->find( button_name )->get_rect();
	item->cost_window_ptr = new t_text_window( get_font( rect.height() ), rect,
	  	                 bg_window_ptr, format_string ("%i", get_cost (item_type) ), t_pixel_24(0,0,0) );
	item->cost_window_ptr->set_center_horizontal();

}

/*
** Show creature windows.
*/
void t_blacksmith_window::create_creature_windows( )
{
	t_adventure_map &				  map = *m_town->get_map();
	int								  player_number = map.get_player_number();
	bool							  foreigners = ( m_town->get_owner_number() != player_number );

	t_screen_point bottom_point;
	t_creature_array_window::t_layout first_row_layout;
	t_screen_rect rect = m_background_bitmaps_ptr->find( "army_display" )->get_rect();

	bottom_point = rect.top_left();
	//bottom_point.y += ((rect.height())/2);
	bottom_point.y += k_creature_top_window_height; //(m_garrison_window_ptr->get_height());
    
    /*
    ** Show visiting heroes - show bottom first because of graphic overlap.
    */
    if (m_adj_army_ptr)
    {
    	m_visitor_window_ptr = new t_creature_array_window( bottom_point, &m_adj_army_ptr->get_creatures(),
                                            t_creature_array_window::k_bottom_row, 0, this );
    	m_visitor_window_ptr->set_select_handler( bound_handler( *this, &t_blacksmith_window::hero_select ) );
	    m_visitor_window_ptr->set_double_click_handler( bound_handler( *this,
			                                      &t_blacksmith_window::creature_double_click ));
		first_row_layout = t_creature_array_window::k_top_row;

		if ( m_adj_army_ptr->get_owner_number() != player_number )
			foreigners = true;

		if ( foreigners )
			setup_foreign_creature_array_window_controls( map, m_visitor_window_ptr );
    }
    else
    {
        // Maxed out armies.
        m_visitor_window_ptr = NULL;
		first_row_layout = t_creature_array_window::k_single_row;
	    rect = m_background_bitmaps_ptr->find( "army_display_single" )->get_rect();
    }
    
    /*
    ** Show garrison heroes.
    */

	m_garrison_window_ptr = new t_creature_array_window( rect.top_left(), &m_town->get_garrison(),
										    first_row_layout, 0, this );
	m_garrison_window_ptr->set_select_handler( bound_handler( *this, &t_blacksmith_window::hero_select ));
	m_garrison_window_ptr->set_double_click_handler( bound_handler( *this,
			                                      &t_blacksmith_window::creature_double_click ));

	if ( foreigners )
		setup_foreign_creature_array_window_controls( map, m_garrison_window_ptr );

	if ( m_visitor_window_ptr )
		m_garrison_window_ptr->link_selection( m_visitor_window_ptr );

    // Selected hero portrait.
	t_bitmap_layer const*   layer;
	t_window*               window;
	layer = m_background_bitmaps_ptr->find( "large_portrait_frame" );
	window = new t_bitmap_layer_window( layer, t_screen_point(0,0), this );
    
    // Current creature place holder...select_first will set picture.
	m_selected_creature_window_ptr = NULL;
	rect = m_background_bitmaps_ptr->find( "hero_portrait" )->get_rect(); // + origin;
    m_selected_creature_window_ptr = new t_bitmap_layer_window( NULL, rect.top_left(), this );

    /*
    ** Select a creature...if there is a visiting army, select it...
    */
    if ((m_adj_army_ptr)) //&& (m_visitor_window_ptr->get_selected_slot() >= 0))
    {
   	    m_visitor_window_ptr->select_first();
        m_selected_window_ptr = m_visitor_window_ptr;
        if (m_visitor_window_ptr->get_selected_slot() < 0)
        {
	        m_garrison_window_ptr->select_first();
            m_selected_window_ptr = m_garrison_window_ptr;
        }
    }
    else
    {
        // Default select hero..
	    m_garrison_window_ptr->select_first();
        m_selected_window_ptr = m_garrison_window_ptr;
    }
}

// Add amount to running total.
void t_blacksmith_window::add_to_total_cost(int amount)
{

    m_current_cost += amount;
	m_cost_window_ptr->set_text( format_string( "%i", m_current_cost ) );

	if (m_current_cost == 0)
    {
	    m_buy_button_ptr->enable (false);
    }
    // If current_cost > gold, disable buy button.
    else if (m_current_cost > m_player_funds)
    {
	    m_buy_button_ptr->enable (false);
    }
    else
    {
	    m_buy_button_ptr->enable (true);
    }
}

// Subtract from running amount.
void t_blacksmith_window::subtract_from_total_cost(int amount)
{

    m_current_cost -= amount;
	m_cost_window_ptr->set_text( format_string( "%i", m_current_cost ) );

	if (m_current_cost == 0)
    {
	    m_buy_button_ptr->enable (false);
    }
    // If current_cost < gold, enable buy button.
    else if (m_current_cost <= m_player_funds)
    {
	    m_buy_button_ptr->enable (true);
    }
    else
    {
	    m_buy_button_ptr->enable (false);
    }
}

/*
** Increment quantity.
*/
void t_blacksmith_window::item_clicked_up( t_button *button_ptr, t_blacksmith_item *item )
{
    if (m_selected_window_ptr->get_selected_slot() < 0)
        return;

    if (item->quantity < k_blacksmith_quantity_limit)
    {
        int item_cost = get_cost ( item->item_type );

        // Increase quantity.
        item->quantity++;

        add_to_total_cost(item_cost);

        // Update text quantity text as well.
    	item->number_window_ptr->set_text ( format_string ( "%i", item->quantity));

        check_buy_button_disable();
    }
}

/*
** Decrement quantity.
*/
void t_blacksmith_window::item_clicked_down( t_button *button_ptr, t_blacksmith_item *item )
{
    if (m_selected_window_ptr->get_selected_slot() < 0)
        return;

    // Decrease quantity...check for zero.
    if (item->quantity > 0)
    {
        int item_cost = get_cost ( item->item_type );

        item->quantity--;
        subtract_from_total_cost(item_cost);

	    item->number_window_ptr->set_text ( format_string ( "%i", item->quantity));
    
        check_buy_button_disable();
    }
}

/*
** Buy selected items/potions.
*/
void t_blacksmith_window::buy_click( t_button *button_ptr )
{
    int i = 0;
    int j = 0;
    t_artifact              artifact;
    t_creature_stack        *army_ptr = NULL;

    // Check if enough funds.
	if (m_current_cost > m_player_funds)
    {
        return;
    }

    // Nothing selected?
    if (m_selected_window_ptr->get_selected_slot() < 0)
        return;

    // Get selected creature.
    if ((army_ptr = get_selected_creature()) == NULL)
        return;

    /*
    ** Go through all items, all potions, and purchase
    ** selected quantity...
    */
    for (i=0; i<k_max_items; i++)
    {
        if (m_items[i].spot_taken == false)
            continue;

        artifact = get_default_artifact( m_items[i].item_type );
        for (j=0; j<m_items[i].quantity; j++)
        {
            army_ptr->add ( artifact );
        }
        // Reset values.
        m_items[i].quantity = 0;
        m_items[i].number_window_ptr->set_text ( format_string ( "0" ) );
    }
    for (i=0; i<k_max_potions; i++)
    {
        if (m_potions[i].spot_taken == false)
            continue;

        artifact = get_default_artifact( m_potions[i].item_type );
        for (j=0; j<m_potions[i].quantity; j++)
        {
            //hero_ptr->add ( artifact );
            army_ptr->add ( artifact );
        }
        // Reset values.
        m_potions[i].quantity = 0;
        m_potions[i].number_window_ptr->set_text ( format_string ( "0" ) );
    }

    // Adjust amount.
	m_player_ptr->spend( k_gold, m_current_cost );
	m_player_funds -= m_current_cost;
    subtract_from_total_cost(m_current_cost);

	t_screen_rect rect = m_background_bitmaps_ptr->find( "kingdom_gold_text" )->get_rect();
	m_total_gold_text_ptr->set_text( abbreviate_number(m_player_funds, rect.width()) );
}

t_creature_stack *t_blacksmith_window::get_selected_creature()
{
    t_creature_stack        *army_ptr = NULL;
	t_creature_array&       army = *m_selected_window_ptr->get_army();

    // Nothing selected?
    if (m_selected_window_ptr->get_selected_slot() < 0)
        return (NULL);

    if (m_selected_window_ptr == m_garrison_window_ptr)
    {
        //hero_ptr = army[m_garrison_window_ptr->get_selected_slot()].get_hero();
        army_ptr = &army[m_garrison_window_ptr->get_selected_slot()];
    }
    else if (m_selected_window_ptr == m_visitor_window_ptr)
    {
        //hero_ptr = army[m_visitor_window_ptr->get_selected_slot()].get_hero();
        army_ptr = &army[m_visitor_window_ptr->get_selected_slot()];
    }

    return (army_ptr);
}

// Show selected picture.
void  t_blacksmith_window::show_current_creature( )
{
    t_creature_stack    *army_ptr = NULL;
    
    if ((army_ptr = get_selected_creature()) == NULL)
        return;

	if (army_ptr->get_number() == 0)
		return;
	m_selected_creature_window_ptr->set_bitmap( army_ptr->get_portrait(82) );
    m_selected_creature_window_ptr->set_help_balloon_text( army_ptr->get_name() );
}

/*
** Clicked on a hero.
*/
void t_blacksmith_window::hero_select( t_creature_array_window* window_ptr, int slot )
{
    /*
    ** Reset all quantities.
    ** Go through all items, all potions...
    */
#if (0)
    int i;
    for (i=0; i<k_max_items; i++)
    {
        if (m_items[i].spot_taken)
        {
            m_items[i].quantity = 0;
	        m_items[i].number_window_ptr->set_text ( format_string ( "0" ) );
        }
    }
    for (i=0; i<k_max_potions; i++)
    {
        if (m_potions[i].spot_taken)
        {
            m_potions[i].quantity = 0;
	        m_potions[i].number_window_ptr->set_text ( format_string ( "0" ) );
        }
    }

    // Reset running total.
    subtract_from_total_cost(m_current_cost);
#endif

    m_selected_window_ptr = window_ptr;

    if (slot == -1)
        return;

#if (0)
    if (m_visitor_window_ptr)
        m_visitor_window_ptr->update();
    m_garrison_window_ptr->update();
#endif

    show_current_creature();
    m_buy_button_ptr->enable (true);
    check_buy_button_disable();
}

// Disable buy button if nothing selected.
bool t_blacksmith_window::check_buy_button_disable()
{
    t_creature_stack        *army_ptr = NULL;

    // Nothing selected?
    if (m_selected_window_ptr->get_selected_slot() < 0)
    {
        m_buy_button_ptr->enable (false);
        return (true);
    }

	// Don't allow player to buy stuff for enemy armies
	// Warning! m_selected_window_ptr->get_army()->get_owner() may return
	// NULL because the army is not yet placed on the map
	t_adventure_map * map = m_town->get_map();
	t_player const * player = &map->get_player();
	t_player const * selected_army_player = &map->get_player( m_selected_window_ptr->get_army()->get_owner_number() );

	if ( !same_team( selected_army_player, player ) )
	{
        m_buy_button_ptr->enable (false);
        return (true);
	}

    // Get selected creature.
    if ((army_ptr = get_selected_creature()) == NULL)
    {
        m_buy_button_ptr->enable (false);
        return (true);
    }

	if (m_current_cost == 0)
    {
	    m_buy_button_ptr->enable (false);
        return (true);
    }

    return (false);
}

void t_blacksmith_window::creature_double_click( t_creature_array_window* window, int slot )
{
    if (m_selected_window_ptr->get_selected_slot() < 0)
        return;

#if (0)
	t_window* dialog = new t_army_dialog_town( m_town, m_adj_army_ptr, m_frame, m_garrison_window_ptr->get_army(), m_town, slot );
	dialog->run_modal();
	if (m_garrison_window_ptr)
	    m_garrison_window_ptr->update();
	if (m_visitor_window_ptr)
	    m_visitor_window_ptr->update();
    m_selected_creature = m_selected_window_ptr->get_selected_slot();
    check_buy_button_disable();
    if (m_selected_window_ptr->get_selected_slot() < 0)
        m_selected_window_ptr->select_first();
    show_current_creature();
#endif
}

void t_blacksmith_window::close_click( t_button *button_ptr )
{
	close();
}

/*-------------------------------------------------------------------------------------------*\
**
** Blacksmith buttons class.
**
\*-------------------------------------------------------------------------------------------*/
t_blacksmith_button::t_blacksmith_button( t_button *icon_button_ptr, t_cached_ptr<t_button_bitmaps>& bitmaps, t_screen_point point,
        t_window* parent, std::string const& text )
            : t_button( bitmaps, point, parent, text )
{
	m_icon_button_ptr = icon_button_ptr;
}

// --------------------------------------------------------
// handle a left button down
// --------------------------------------------------------
void t_blacksmith_button::left_button_down( t_mouse_event const& event )
{
    if (m_pressed || m_disabled)
        return;
    m_pressed = true;
	set_image();
	m_icon_button_ptr->set_pressed ();
}

// --------------------------------------------------------
// handle a left button release
// --------------------------------------------------------
void t_blacksmith_button::left_button_up( t_mouse_event const& event )
{
    if (!m_pressed)
       return;
    m_pressed = false;
	set_image();
	m_clicked( this );
	m_icon_button_ptr->set_pressed(false); //highlighted (false);
}

// --------------------------------------------------------
// handle the mouse leaving the button area
// --------------------------------------------------------
void t_blacksmith_button::mouse_leaving( t_window* old_window, t_window* new_window,
		                            t_mouse_event const& event )
{
	if (is_child( new_window ))
		return;

	if (m_pressed || m_highlighted)
	{
		m_pressed = false;
	    m_highlighted = false;
		set_image();
	    m_icon_button_ptr->set_pressed(false);
	}
}
